﻿/// <reference path="../_references.js" />

// long text => text...
function reduce_text(string, length) {
    if (string.length <= length)
        return string;
    else
        return string.replace(string.substr(length, string.length), "...");
}

// check number only
function OnlyNumeric(e) {
    if ((e.which < 48 || e.which > 57)) {
        if (e.which == 8 || e.which == 46 || e.which == 0) {
            return true;
        }
        else {
            return false;
        }
    }
}

// change img upload
function ChangeImage(idUpload, idImage) {
    idUpload = "#" + idUpload;
    idImage = "#" + idImage;
    // change image
    $(idUpload).change(function (evt) {
        var f = evt.target.files[0];
        var reader = new FileReader();

        if (!f.type.match('image.*')) {
            alert("The selected file does not appear to be an image.");
            $(idUpload).val('');
            $(idImage).attr('src', '../Images/avatars/defaut-avatar.png');
            return;
        }

        reader.readAsDataURL(f);
        reader.onload = function (e) {
            $('#avatar-image').attr('src', e.target.result);
        };
    });
}

// refesh image
function RefreshCaptcha(idButton, idImage, imageLink) {
    idButton = "#" + idButton;
    idImage = "#" + idImage;
    // refresh captcha
    $(idButton).click(function () {
        $.ajax({
            url: imageLink,
            type: 'POST',
            success: function (data) {
                $(idImage).attr("src", imageLink);
            }
        });
    });
}

// Lấy querystring parameter
function getQueryStringParameter(key) {
    var re = new RegExp("([\\?&])" + key + "=.*?(&|$)", "i");
    if (location.search.match(re)) {
        var value = location.search.match(re)[0];
        value = value.substring(value.indexOf("=") + 1).replace("&", "");
        return value;
    }
    return 0; 
}

// set querystring parameter
function updateQueryStringParameter(uri, key, value) {
    var re = new RegExp("([\\?&])" + key + "=.*?(&|$)", "i");
    var separator = uri.indexOf('?') !== -1 ? "&" : "?";
    if (uri.match(re)) {
        return uri.replace(re, '$1' + key + "=" + value + '$2');
    }
    else {
        return uri + separator + key + "=" + value;
    }
}

// hidden when submit
function OnSubmit_Click(idButton) {
    var bt = document.getElementById(idButton);
    if ($(bt.form).valid()) {
        bt.form.submit();
        bt.disable = true;
        bt.value = "Sending...";
    }
}

//#region paging
//<div class="page-size">
//    <select>
//        <optgroup label="Page size">
//            <option value="10">10</option>
//            <option value="20">20</option>
//            <option value="100">100</option>
//        </optgroup>
//    </select>
//</div>

//<script>
//    $("#page1 .page-size select").on("change", function () {
//        onPageSizeChange(this.valueOf());
//    });

//function onPageSizeChange(pageSize) {
//    var link = updateQueryStringParameter(document.URL, 'pagesize', pageSize);
//    location.href = link;
//}
//</script>
//#endregion
